package servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "Demo12_AjaxServlet", value = "/ajax")
public class Demo12_AjaxServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String backString = "";

        String keyword = request.getParameter("keyword");
        if(keyword.equals("ajax")){
            backString = "你好，ajax";
        }else if(keyword.equals("ajaxGet")){
            backString = "你好，ajaxGet";
        }else if(keyword.equals("ajaxPost")){
            backString = "你好，ajaxPost";
        }else if(keyword.equals("ajaxGetJson")){
            //注意：json格式外侧一定要加 {}
            backString = "{\"mess1\":\"你好\",\"mess2\":\"ajaxGetJson\"}";
        }else if(keyword.equals("ajaxLoad")){
            backString = "你好，load";
        }

        System.out.println("=========================="+backString);

        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.print(backString);
        writer.close();

    }
}
